package com.cn.mobileproject.view.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.cn.mobileproject.R;
import com.cn.mobileproject.bean.Project;
import com.cn.mobileproject.data.GlobalValue;

public class ProjectAdapter extends BaseAdapter{
protected Context mContext;
	
	public ProjectAdapter(Context context){
		mContext = context;
	}
	
	protected ArrayList<Project> mList ;
	
	@Override
	public int getCount() {
		if(mList==null) return 0;
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	class ViewHolder{
		TextView nameText;
		TextView planStartDateText;
		TextView PlanEndDateText;
		TextView codeText;
		TextView cistricatAreaText;
		Button statusBtn;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.project_item, null);
			holder = new ViewHolder();
			holder.nameText =(TextView) convertView.findViewById(R.id.project_item_text_name);
			holder.planStartDateText =(TextView) convertView.findViewById(R.id.project_item_text_time);
			holder.PlanEndDateText =(TextView) convertView.findViewById(R.id.project_item_text_time2);
			holder.codeText =(TextView) convertView.findViewById(R.id.project_item_text_code);
			holder.cistricatAreaText =(TextView) convertView.findViewById(R.id.project_item_text_unitname);
			holder.statusBtn =(Button) convertView.findViewById(R.id.project_item_text_icon);
			convertView.setTag(holder);
		}
		else{
			holder = (ViewHolder) convertView.getTag();
		}
		if(position>mList.size()||holder==null) return convertView;
		Project project = mList.get(position);
		holder.nameText.setText(project.name);
		holder.planStartDateText.setText(project.planStartDate);
		holder.PlanEndDateText.setText(project.planEndDate);
		holder.codeText.setText(project.code);
		holder.cistricatAreaText.setText(project.districtAreaValue);
		Project defaultProject = GlobalValue.getInstance().getDefaultProject();
		if(project!=null&&defaultProject!=null){
			if(project.equals(defaultProject)){
				holder.statusBtn.setVisibility(View.VISIBLE);
			}
			else holder.statusBtn.setVisibility(View.GONE);
		}
		else holder.statusBtn.setVisibility(View.GONE);
		return convertView;
	}

	public ArrayList<Project> getList() {
		return mList;
	}

	public void setList(ArrayList<Project> list){
		if(mList==null){
			mList = new ArrayList<Project>();
		}
		mList.clear();
		if(list!=null){
			mList.addAll(list);
		}
	}
}
